package com.zsk.shop.controller.gop;

import com.zsk.shop.api.order.OrderStatus;
import com.zsk.shop.common.annotion.NoPermission;
import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.base.model.token.TokenInfoModel;
import com.zsk.shop.common.base.page.BaseDateQueryParams;
import com.zsk.shop.common.utils.DateUtils;
import com.zsk.shop.common.utils.EntityConvertUtils;
import com.zsk.shop.common.utils.TokenUtil;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.common.utils.excel.ExcelUtil;
import com.zsk.shop.model.excel.WaitDeliveryOrderExportModel;
import com.zsk.shop.model.params.*;
import com.zsk.shop.model.results.*;
import com.zsk.shop.service.DeliveryAddressStatusService;
import com.zsk.shop.service.GopOrderService;
import com.zsk.shop.service.ShopGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author Ys
 * @date 2022年01月25日 10:21
 */
@RestController
@Api(tags = "GOP-订单相关")
@RequestMapping("/gop/groupPurchasing")
public class GopOrderController {

    @Resource
    private DeliveryAddressStatusService deliveryAddressStatusService;
    @Resource
    private GopOrderService gopOrderService;
    @Resource
    private ShopGoodsService shopGoodsService;

    @ApiOperation("GOP订单列表")
    @PostMapping(value = "/getGopOrderList.do")
    public BaseResponse<OrderListResult> getGopOrderList(@RequestBody OrderParam param) {
        return gopOrderService.getGopOrderList(param);
    }

    @PostMapping("/getOrderList.do")
    @ApiOperation("获取报单列表")
    public BaseResponse getOrderList(@RequestBody GopOrderParams params) {
        return gopOrderService.getOrderList(params);
    }

    @ApiOperation("订单详情")
    @GetMapping(value = "/getGopOrderInfo.do")
    @ApiImplicitParam(name = "orderNo", value = "订单编号")
    public BaseResponse<OrderInfoResult> getOrderInfo(String orderNo) {
        return gopOrderService.getOrderInfo(orderNo);
    }

    @PostMapping("/addOrder.do")
    @ApiOperation("新增报单")
    public BaseResponse addOrder(@RequestBody GopSaveOrderParams params) {
        return gopOrderService.add(params);
    }

    @NoPermission
    @ApiOperation(value = "报单时商品列表")
    @PostMapping("/getGoodsList.do")
    public BaseResponse<List<GopGoodsListResult>> getGoodsList(@RequestBody GopGoodsListParams param) {
        return gopOrderService.getGoodsList(param);
    }

    @PostMapping("/saveOrderNumber.do")
    @ApiOperation("添加包裹物流单号")
    public BaseResponse saveOrderNumber(@RequestBody PackageOrderParams params) {
        return deliveryAddressStatusService.saveOrderNumber(params);
    }

    @GetMapping("/queryNumber.do")
    @ApiOperation("查看单号是否已录入")
    public BaseResponse queryNumber(String number) {
        return deliveryAddressStatusService.queryNumber(number);
    }

    @GetMapping("/getPackageList.do")
    @ApiOperation("查看物流包裹列表")
    public BaseResponse<PackageResult> getPackageList(String orderNo) {
        return deliveryAddressStatusService.getPackageList(orderNo);
    }

    @GetMapping("/getOrderStatus.do")
    @ApiOperation("获取订单物流信息")
    public BaseResponse getOrderStatus(String number) {
        return BaseResponse.success(OrderStatus.getOrderInfo(number));
    }

    @NoPermission
    @ApiOperation("Gop订单列表-导出")
    @GetMapping("/waitDeliveryOrderExport.do")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间")})
    public void waitDeliveryOrderExport(HttpServletResponse response, String orderStatus, String orderNo, String phone, String goodsName, String startTime, String endTime, String token) {
        TokenInfoModel tokenInfoModel = TokenUtil.verifyToken(token);
        if (ToolUtil.isEmpty(tokenInfoModel)) {
            return;
        }
        List<WaitDeliveryOrderResult> result = gopOrderService.waitDeliveryOrder(orderStatus, orderNo, phone, goodsName, startTime, endTime);
        List<WaitDeliveryOrderExportModel> results = EntityConvertUtils.convertAListToBList(result, WaitDeliveryOrderExportModel.class);
        String fileName = "未发货订单(导出时间：" + DateUtils.format(new Date(), DateUtils.YYYYMMddHHmm) + ")";
        if (!ToolUtil.isAllEmpty(startTime, endTime)) {
            fileName = "未发货订单(" + startTime.replaceAll(":", "") + "-" + startTime.replaceAll(":", "") + ")";
        }
        ExcelUtil.writeExcel(response, results, fileName, new WaitDeliveryOrderExportModel());
    }

    @NoPermission
    @ApiOperation(value = "销售统计")
    @PostMapping("/saleInfo.do")
    public BaseResponse<List<GopSaleInfoListResult>> saleInfo(@RequestBody GopSaleInfoListParams param) {
        return gopOrderService.saleInfo(param);
    }

    @NoPermission
    @ApiOperation(value = "统计明细")
    @PostMapping("/saleDetail.do")
    public BaseResponse<SaleDetailResult> saleDetail(@RequestBody BaseDateQueryParams param) {
        return gopOrderService.saleDetail(param);
    }

}
